package com.jx.lcs.productorder.service;

import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jx.lcs.productorder.dao.ProductOrderDao;
import com.jx.lcs.productorder.entity.ProductOrder;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.service.BaseService;

/** 
 * 
 * @author JiaYY 
 * @date 2015年7月15日
 */
@Service
@Transactional(readOnly=true)
public class ProductOrderService extends BaseService<ProductOrder, String> {

	@Autowired
	private ProductOrderDao productOrderDao;
	
	@Override
	public HibernateDao<ProductOrder, String> getEntityDao() {
		return productOrderDao;
	}
	
	public Page<ProductOrder> findProductOrderList(Page<ProductOrder> page, HttpServletRequest request) throws Exception {
		String orderId = request.getParameter("orderId");
		String pname = request.getParameter("pname");
		String pcode = request.getParameter("pcode");
		String userName = request.getParameter("userName");
		String uid = request.getParameter("uid");
		String cname = request.getParameter("cname");
		String cid = request.getParameter("cid");
		String status = request.getParameter("status");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		
		StringBuilder sb = new StringBuilder();
		sb.append("from ProductOrder t where t.user.id != null and t.productInfo.id != null");
		
		if(orderId != null && !"".equals(orderId.trim())) {
			sb.append(" and t.id = '").append(orderId).append("'");
		}
		if(pname != null && !"".equals(pname.trim())) {
			pname = URLDecoder.decode(pname, "UTF-8");
			sb.append(" and t.productInfo.pname like '%").append(pname).append("%'");
		}
		if(pcode != null && !"".equals(pcode.trim())) {
			sb.append(" and t.productInfo.pcode like '%").append(pcode).append("%'");
		}
		if(userName != null && !"".equals(userName.trim())) {
			userName = URLDecoder.decode(userName, "UTF-8");
			sb.append(" and t.user.userName like '%").append(userName).append("%'");
		}
		if(uid != null && !"".equals(uid.trim())) {
			sb.append(" and t.user.uid like '%").append(uid).append("%'");
		}
		if(cname != null && !"".equals(cname.trim())) {
			cname = URLDecoder.decode(cname, "UTF-8");
			sb.append(" and t.cname like '%").append(cname).append("%'");
		}
		if(cid != null && !"".equals(cid.trim())) {
			sb.append(" and t.cid like '%").append(cid).append("%'");
		}
		if(status != null && !"".equals(status.trim())) {
			sb.append(" and t.status = '").append(status).append("'");
		}
		if(beginDate != null && !"".equals(beginDate.trim())) {
			sb.append(" and t.orderTime > '").append(beginDate).append("'");
		}
		if(endDate != null && !"".equals(endDate.trim())) {
			sb.append(" and t.orderTime <= '").append(endDate).append(" 23:59:59'");
		}
		sb.append(" order by t.orderTime desc");
		
		return productOrderDao.findPage(page, sb.toString(), new Object[]{});
	}

}
